package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接工厂
        ConnectionFactory cf=new ConnectionFactory();
        cf.setHost("192.168.126.130");
        cf.setUsername("admin");
        cf.setPassword("admin");
        //建立连接
        Connection connection = cf.newConnection();
        //建立通道
        Channel ch = connection.createChannel();
        //声明队列
        ch.queueDeclare("helloworld",false,false,false,null);
        System.out.println("wait~~~~data");
        //收到消息后用来处理消息的回调对象
        DeliverCallback callback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody(),"UTF-8");
                System.out.println("收到"+msg);
                }
        };

        //消费者取消时的回调对象
        CancelCallback cancel = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        ch.basicConsume("helloworld",true,callback,cancel);
    }
}
